home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / SCREEN / CURSES01 / minix / c / strget < prev    next >
Text File  |  1991-05-05  |  6KB  |  184 lines

  1. /****************************************************************/
  2. /* Getstr() routines of the PCcurses package            */
  3. /*                                */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version    */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,    */
  8. /* and therefore consider myself free to make it public domain.    */
  9. /*        Bjorn Larsson (...mcvax!enea!infovax!bl)    */
  10. /****************************************************************/
  11. /* 1.0:    Release:                    870515    */
  12. /****************************************************************/
  13. /* Modified to run under the MINIX operating system by Don Cope */
  14. /* These changes are also released into the public domain.      */
  15. /*                             900906  */
  16. /****************************************************************/
  17.  
  18. #include <curses.h>
  19. #include "curspriv.h"
  20.  
  21. static    char    *backchar();
  22.  
  23. static    bool     oldecho;
  24. static    bool     oldcbreak;
  25. static  bool     oldnodelay;
  26. static    char    *strbeg;
  27. static    WINDOW  *w;
  28. static    int     xbeg;
  29.  
  30. /****************************************************************/
  31. /* Wgetstr(win,str) reads in a string (terminated by \n or \r)    */
  32. /* to the buffer pointed to by 'str', and displays the input    */
  33. /* in window 'win'. The user's erase and kill characters are    */
  34. /* active.                            */
  35. /****************************************************************/
  36.  
  37. int wgetstr(win,str)
  38.   WINDOW    *win; 
  39.   char        *str;
  40.   {
  41.   w          = win;
  42.   strbeg      = str;        /* keep start for backspacing */
  43.   oldcbreak       = _cursvar.cbreak;    /* remember states */
  44.   oldecho         = _cursvar.echo;
  45.   oldnodelay      = w->_nodelay;
  46.   _cursvar.echo   = FALSE;        /* we do echo ourselves */
  47.   _cursvar.cbreak = TRUE;        /* no wait for chars */
  48.   w->_nodelay   = FALSE;        /* don't return 'NOCHARS' */
  49.   xbeg = w->_curx;            /* remember screen start x-position */
  50.  
  51.   wrefresh(w);                /* sets cursor at right place */
  52.   while ((*str = getch()) != '\n')
  53.     {
  54.     if (*str == '\r')
  55.       break;
  56.     if (*str == _DCCHAR)
  57.       {
  58.       if (str > strbeg)
  59.     str = backchar(str);
  60.       } /* if */
  61.     else
  62.       if (*str == _DLCHAR)
  63.     while (str > strbeg)
  64.       str = backchar(str);
  65.       else
  66.     {
  67.     waddch(w,*str++);
  68.     wrefresh(w);
  69.     } /* else */
  70.       } /* while */
  71.  
  72.   *str = '\0';
  73.   _cursvar.echo   = oldecho;
  74.   _cursvar.cbreak = oldcbreak;
  75.   win->_nodelay   = oldnodelay;
  76.   return(OK);
  77.   } /* wgetstr */
  78.  
  79. /****************************************************************/
  80. /* Getstr(str) reads in a string (terminated by \n or \r) to    */
  81. /* the buffer pointed to by 'str', and displays the input in    */
  82. /* stdscr. The user's erase and kill characters are active.    */
  83. /****************************************************************/
  84.  
  85. int getstr(str)
  86.   char *str;
  87.   {
  88.   return(wgetstr(stdscr,str));
  89.   } /* getstr */
  90.  
  91. /****************************************************************/
  92. /* Mvgetstr(y,x,str) moves the stdscr cursor to a new position,    */
  93. /* then reads in a string (terminated by \n or \r) to the buf-    */
  94. /* fer pointed to by 'str', and displays the input in stdscr.    */
  95. /* The user's erase and kill characters are active.        */
  96. /****************************************************************/
  97.  
  98. int mvgetstr(y,x,str)
  99.   int y;
  100.   int x;
  101.   char *str;
  102.   {
  103.   if (wmove(stdscr,y,x) == ERR)
  104.     return(ERR);
  105.   return(wgetstr(stdscr,str));
  106.   } /* mvgetstr */
  107.  
  108. /****************************************************************/
  109. /* Mvwgetstr(win,y,x,str) moves the 'win' cursor to a new    */
  110. /* position, then reads in a string (terminated by \n or \r)    */
  111. /* to the buffer pointed to by 'str', and displays the input in    */
  112. /* stdscr. The user's erase and kill characters are active.    */
  113. /****************************************************************/
  114.  
  115. int mvwgetstr(win,y,x,str)
  116.   WINDOW *win;
  117.   int      y;
  118.   int      x;
  119.   char     *str;
  120.   {
  121.   if (wmove(win,y,x) == ERR)
  122.     return(ERR);
  123.   return(wgetstr(win,str));
  124.   } /* mvwgetstr */
  125.  
  126. /****************************************************************/
  127. /* Backchar() does a character delete with screen erase, even    */
  128. /* up to previous lines. It will not back-scroll if the begi-    */
  129. /* ning of the string has scrolled off the window. Steps back    */
  130. /* pointer 's', and returns the new value.            */
  131. /****************************************************************/
  132.  
  133. static char *backchar(s)
  134.   char      *s;
  135.   {
  136.   static int nbs;
  137.   static int x;
  138.   static char *p;
  139.   static int ts;
  140.  
  141.   x =  xbeg;
  142.   ts =  w->_tabsize;
  143.  
  144.   s--;                        /* step back string */
  145.   nbs = 1;                    /* step at least one pos */
  146.   if ((*s < ' ') || (*s == 0x7f))        /* ctrl-char has size 2 */
  147.     nbs++;
  148.   if (*s == '\t')                /* tabs are very special */
  149.     {
  150.     for (p = strbeg; p < s ;p++)        /* find x-pos of last char */
  151.       {
  152.       if (*p == '\t')                /* go to next tab */
  153.     x = ((x/ts)+1) * ts;
  154.       else
  155.     if ((*p < ' ') || (*p == 0x7f))        /* control character */
  156.       x += 2;
  157.     else                    /* normal char */
  158.       x++;
  159.       if (x > w->_maxx)                /* go to next line? */
  160.     x = 0;
  161.       } /* while */
  162.     if (!(w->_curx))                /* if step-over newline */
  163.       nbs = w->_maxx+1 - x;
  164.     else                    /* in-line tab */
  165.       nbs = w->_curx - x;            /* positions to erase */
  166.     } /* if */
  167.  
  168.   while(nbs--)                    /* do so many */
  169.     {
  170.     if (w->_curx > 0)                /* if not at line begining */
  171.       waddstr(w,"\b \b");
  172.     else
  173.       if (w->_cury)                /* if not on top line */
  174.     {
  175.     mvwaddch(w,w->_cury-1,w->_maxx,' ');    /* put space at line end */
  176.     wmove(w,w->_cury-1,w->_maxx);        /* and go there again */
  177.     } /* else */
  178.     } /* while */
  179.  
  180.   wrefresh(w);                    /* redraw screen */
  181.   *(s+1) = '\0';                /* make string terminated */
  182.   return(s);
  183.   } /* backchar */
  184.